﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MangioneManagement.Models;

namespace MangioneManagement.Controllers
{
    public class TransactionController : Controller
    {
        private MangioneManagementDbContext db = new MangioneManagementDbContext();
        //
        // GET: /Transaction/

        public ViewResult Index()
        {
#if DEBUG == true
            return View(new List<Transaction>{
                                 new Transaction{Customer = new Customer{ Country = "Argentina",
                                 CustomerID = 1,
                                 Email="hernan.zbucki@gmail.com",
                                 FirstName="Hernan",
                                 LastName="Zbucki",
                                 Locality="CABA",
                                 MlUser="CEZBU",
                                 MobilePhone="1164529330",
                                 Phone="20534439",
                                 Province="CABA"           },
                                 Date = DateTime.Now,
                                 Product = new Product{Brand="Yamaha",LogicalStock=10,Model="XBR345",PhysicalStock=5,ProductId=1},
                                 TransactionId = 1,                              
                                 Username = "hernan"
                                 },
                                 new Transaction{Customer = new Customer{ Country = "Argentina",
                                 CustomerID = 1,
                                 Email="hernan.zbucki@gmail.com",
                                 FirstName="Hernan",
                                 LastName="Zbucki",
                                 Locality="CABA",
                                 MlUser="CEZBU",
                                 MobilePhone="1164529330",
                                 Phone="20534439",
                                 Province="CABA"           },
                                 Date = DateTime.Now,
                                 Product = new Product{Brand="Yamaha",LogicalStock=10,Model="XBR345",PhysicalStock=5,ProductId=1},
                                 TransactionId = 2,                              
                                 Username = "Mariano"
                                 }});
#else
            return View(db.Transactions.ToList());
#endif
        }

        [HttpPost]
        public ActionResult Index(string submit)
        {
            return RedirectToAction("Create");
        }

        [HttpGet, ActionName("Create")]
        public ActionResult Create_SelectCustomer(string customerId)
        {
            ViewBag.TotalSteps = 3;
            ViewBag.CurrentStep = 1;
            if (String.IsNullOrEmpty(customerId))
            {
                ViewBag.IsCustomerSelected = false;
                ViewBag.SelectedCustomer = "No hay cliente seleccionado aún. Seleccione uno o genere uno nuevo";
            }
            else
            {
                ViewBag.IsCustomerSelected = true;
#if DEBUG == true
                Session["SelectedCustomer"] = db.Customers.Find(c => c.CustomerID.ToString() == customerId);
#else
                Session["SelectedCustomer"] = db.Customers.Find(Convert.ToInt32(customerId));
#endif

                ViewBag.SelectedCustomer = "<b>Cliente seleccionado:</b>" + Session["SelectedCustomer"].ToString();
            }

            return View();
        }
        [HttpGet, ActionName("Create_Step2")]
        public ActionResult Create_SelectProduct(string productId)
        {
            ViewBag.TotalSteps = 3;
            ViewBag.CurrentStep = 2;
          
            return View();
        }
        [HttpPost]
        public ActionResult Create(string submit)
        {
            switch (submit)
            {
                case "Continuar - Elegir Producto":
                    if (Session["SelectedCustomer"] == null)
                    {
                        ViewBag.ErrorMessage = "No eligió un cliente válido para la transacción";
                        return View();
                    }
                    else
                    {
                        return RedirectToAction("Create_Step2");
                    }
                case "Buscar Cliente":
                    return RedirectToAction("Index", "Customer", new { mod = "selectOneCust" });
                case "Crear Cliente":
                    return RedirectToAction("Create", "Customer", new { mod = "selectOneCust" });
                default:
                    return Content("Falta implementar el submit " + submit);
            }
        }
    }
}
